package com.xinnet.mms.order.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xinnet.core.utils.page.PageView;
import com.xinnet.mms.config.dao.FlowTurnConfigDao;
import com.xinnet.mms.dept.service.DeptService;
import com.xinnet.mms.deptproduct.dao.DeptProductDao;
import com.xinnet.mms.empcustomrelation.dao.EmpCustomRelationDao;
import com.xinnet.mms.entity.AgentConsume;
import com.xinnet.mms.entity.Customer;
import com.xinnet.mms.entity.CustomerCare;
import com.xinnet.mms.entity.CustomerConsume;
import com.xinnet.mms.entity.CustomerConsumptionDetail;
import com.xinnet.mms.entity.EmpCustomRelation;
import com.xinnet.mms.entity.Product;
import com.xinnet.mms.enums.ProductTypeEnum;
import com.xinnet.mms.order.dao.OrderDao;
import com.xinnet.mms.order.service.OrderService;
import com.xinnet.mms.utils.CustomerManage;

@Service("orderService")
public class OrderServiceImpl implements OrderService {

	@Autowired
	private OrderDao orderDao;

	@Autowired
	EmpCustomRelationDao empCustomRelationDao;

	@Autowired
	FlowTurnConfigDao flowTurnConfigDao;
	
	@Autowired
	DeptService deptService;
	
	@Autowired
	DeptProductDao deptProductDao;

	/**
	 * 根据会员id查询该会员的所有订单
	 * 
	 * @throws Exception
	 */
	@Override
	public List<CustomerCare> getOrdersByCusId(String cusId) throws Exception {
		return orderDao.getOrdersByCusId(cusId);
	}

	/**
	 * 根据会员id以及产品ID查询该会员的订单
	 */
	@Override
	public List<CustomerCare> getOrdersByCusIdAndProductCode(String cusId,
			String productCode) throws Exception {
		return orderDao.getOrdersByCusIdAndProductCode(cusId, productCode);
	}

	/**
	 * 新订单处理
	 * 
	 * @throws SQLException
	 */
	public List<CustomerCare> getNewOrder(String startTime, String endTime)
			throws SQLException {
		return orderDao.getNewOrder(startTime, endTime);
	}

	@Override
	public PageView<Customer> queryDueSoonGoods(PageView<Customer> pageView,
			Customer customer) {

		pageView.setRecords(orderDao.queryDueSoonGoods(pageView, customer));
		return pageView;
	}

	@Override
	public Customer getServiceTimeByServiceIdAndProductCode(Customer customer) {

		return orderDao.getServiceTimeByServiceIdAndProductCode(customer);
	}

	public List<CustomerCare> getDeldOrderList(String cusId, String productCode) {

		return orderDao.getDeldOrderList(cusId, productCode);
	}

	@Override
	public int getDueSoonGoodsCount(Customer customer) throws Exception {

		Integer dueWarningTime = Integer.parseInt(flowTurnConfigDao
				.getWarningTime().getConfigValue());
		//设置到期提醒时间
		customer.setDueWarningTime(dueWarningTime);
		
		//获取客户ID集合
		List<String> customIdList = new ArrayList<String>();
		EmpCustomRelation empCustomRelation = new EmpCustomRelation();
		empCustomRelation.setCustomType(CustomerManage.SERVICE_CUSTOMER);
		
		Integer rowCount = 0;
		if(null != customer.getEmpId() && !"".equals(customer.getEmpId())) {
			empCustomRelation.setEmpId(customer.getEmpId());
			List<EmpCustomRelation> empCustomRelationList = empCustomRelationDao.getEmpCustomRelationListByCondition(empCustomRelation);
			if(null != empCustomRelationList && empCustomRelationList.size() > 0) {
				for(EmpCustomRelation e : empCustomRelationList) {
					customIdList.add(e.getCustomId());
				}
				customer.setCustomIdList(customIdList);
			}
			
			//如果没有服务器客户 就没有即将到期客户
			if(null != customIdList && customIdList.size() > 0) {
				rowCount = orderDao.getIdcCount(customer);
				rowCount += orderDao.getIdcServerHostingCount(customer);
				rowCount += orderDao.getMailboxCount(customer);
				rowCount += orderDao.getVhostCount(customer);
				rowCount += orderDao.getChiYunCount(customer);
				rowCount += orderDao.getDomainCount(customer);
				rowCount += orderDao.getJianzhanCount(customer);
			} else {
				return 0;
			}
		}else {//部门负责人查询
			empCustomRelation.setCompanyId(customer.getCompanyId());
			List<EmpCustomRelation> empCustomRelationList = empCustomRelationDao.getEmpCustomRelationListByCondition(empCustomRelation);
			if(null != empCustomRelationList && empCustomRelationList.size() > 0) {
				for(EmpCustomRelation e : empCustomRelationList) {
					customIdList.add(e.getCustomId());
				}
				customer.setCustomIdList(customIdList);
			}
			rowCount = orderDao.getIdcCount(customer);
			rowCount += orderDao.getIdcServerHostingCount(customer);
			rowCount += orderDao.getMailboxCount(customer);
			rowCount += orderDao.getVhostCount(customer);
			rowCount += orderDao.getChiYunCount(customer);
			rowCount += orderDao.getDomainCount(customer);
			rowCount += orderDao.getJianzhanCount(customer);
		}
		return rowCount;
	}

	@Override
	public PageView<Customer> queryBuyGoods(PageView<Customer> pageView,
			Customer customer) {
		int count = orderDao.queryBuyGoodsCount(customer);
		pageView.setRecords(orderDao.queryBuyGoods(pageView, customer));
		customer.setRowCount(count);
		return pageView;
	}

	@Override
	public PageView<Customer> queryOrderByCustomId(PageView<Customer> pageView,
			Customer customer) {
		int count = orderDao.queryOrderByCustomIdCount(customer);
		pageView.setRecords(orderDao.queryOrderByCustomId(pageView, customer));
		customer.setRowCount(count);
		return pageView;
	}

	@Override
	public PageView<CustomerConsumptionDetail> getCustomerConsumptionDetail(
			PageView<CustomerConsumptionDetail> pageView,
			CustomerConsume customerConsume) {
		Integer rowCount = orderDao.getCCDetailCount(customerConsume);
		Integer pageSize = pageView.getPageSize();
		Integer pageCount = (rowCount + pageSize - 1) / pageSize;
		
		List<CustomerConsumptionDetail> ccdetailResult = orderDao.getCustomerConsumptionDetail(pageView,customerConsume);
		
		if(ccdetailResult!=null &&ccdetailResult.size()>0){
			ccdetailResult.get(0).setPageNow(pageView.getPageNow());
			ccdetailResult.get(0).setRowCount(rowCount);
			ccdetailResult.get(0).setPageSize(pageSize);
			ccdetailResult.get(0).setPageCount(pageCount);
		}
		
		pageView.setQueryResult(rowCount, ccdetailResult);
		pageView.setPageCount(pageCount);
		
		return pageView;
	}

	@Override
	public List<AgentConsume> getAgentConsume(CustomerConsume customerConsume) {
		return orderDao.getAgentConsume(customerConsume);
	}

	
}
